clear all
set more off

cd "C:\Users\ot3\Documents\CRFertility\NLSY79"
insheet using NLSY79.csv, comma  case
recode _all (-1 -2 -3 -4 -5=.)

***renames (NLS generated code)***
  rename R0000100 CASEID_1979 
  rename R0000300 Q1_3_A_M_1979   // Q1-3_A~M
  rename R0000500 Q1_3_A_Y_1979   // Q1-3_A~Y
  rename R0001010 FAM_POB_1979   // FAM-POB
  rename R0001610 FAM_RES_1979   // FAM-RES
  rename R0001800 FAM_6_1979   // FAM-6
  rename R0006500 HGC_MOTHER_1979   // HGC-MOTHER
  rename R0006900 FAMOCC_19_1979   // FAMOCC-19
  rename R0007900 HGC_FATHER_1979   // HGC-FATHER
  rename R0008300 FAMOCC_26_1979   // FAMOCC-26
  rename R0009100 FAM_28A_1979   // FAM-28A
  rename R0009300 FAM_28C_1979   // FAM-28C
  rename R0015600 Q3_1B_1979   // Q3-1B
  rename R0155400 Q13_5_1979   // Q13-5
  rename R0156000 Q13_8_1979   // Q13-8
  rename R0169200 INCOME_25A_1979   // INCOME-25A
  rename R0173600 SAMPLE_ID_1979 
  rename R0189100 HHI_36A_1979   // HHI-36A
  rename R0190600 HHS_1_78SCRN   // HHS-1
  rename R0214700 SAMPLE_RACE_78SCRN 
  rename R0214800 SAMPLE_SEX_1979 
  rename R0214900 ESR_KEY_1979 
  rename R0215200 SMSARES_1979 
  rename R0216100 SAMPWEIGHT_1979 
  rename R0216400 REGION_1979 
  rename R0216500 AGEATINT_1979 
  rename R0216701 HGCREV79_1979 
  rename R0228100 Q3_1_1980   // Q3-1
  rename R0228500 Q3_1B_1980   // Q3-1B
  rename R0312300 Q13_5_1980   // Q13-5
  rename R0312600 Q13_9_1980   // Q13-9
  rename R0403000 HH5_10_1980   // HH5-10
  rename R0405700 REGION_1980 
  rename R0406300 ESR_KEY_1980 
  rename R0406401 HGCREV80_1980 
  rename R0406510 AGEATINT_1980 
  rename R0410100 Q1_3_A_M_1981   // Q1-3_A~M
  rename R0410300 Q1_3_A_Y_1981   // Q1-3_A~Y
  rename R0414700 Q3_1_1981   // Q3-1
  rename R0416800 Q3_1B_1981   // Q3-1B
  rename R0482600 Q13_5_1981   // Q13-5
  rename R0483200 Q13_8_1981   // Q13-8
  rename R0602810 REGION_1981 
  rename R0618301 AFQT_3_1981   // AFQT-3
  rename R0618800 ESR_KEY_1981 
  rename R0618901 HGCREV81_1981 
  rename R0619010 AGEATINT_1981 
  rename R0661800 Q3_1_1982   // Q3-1
  rename R0663900 Q3_1B_1982   // Q3-1B
  rename R0782101 Q13_5_TRUNC_REVISED_1982   // Q13-5_TRUNC_REVISED
  rename R0782401 Q13_8_TRUNC_REVISED_1982   // Q13-8_TRUNC_REVISED
  rename R0897910 REGION_1982 
  rename R0898201 HGCREV82_1982 
  rename R0898310 AGEATINT_1982 
  rename R0898500 ESR_KEY_1982 
  rename R0903300 Q3_1_1983   // Q3-1
  rename R0905300 Q3_1B_1983   // Q3-1B
  rename R1024001 Q13_5_TRUNC_REVISED_1983   // Q13-5_TRUNC_REVISED
  rename R1024301 Q13_9_TRUNC_REVISED_1983   // Q13-9_TRUNC_REVISED
  rename R1144700 ESR_KEY_1983 
  rename R1144800 REGION_1983 
  rename R1145001 HGCREV83_1983 
  rename R1145110 AGEATINT_1983 
  rename R1203100 Q3_1_1984   // Q3-1
  rename R1205200 Q3_1B_1984   // Q3-1B
  rename R1410701 Q13_5_TRUNC_REVISED_1984   // Q13-5_TRUNC_REVISED
  rename R1411001 Q13_8_TRUNC_REVISED_1984   // Q13-8_TRUNC_REVISED
  rename R1519900 ESR_KEY_1984 
  rename R1520000 REGION_1984 
  rename R1520201 HGCREV84_1984 
  rename R1520310 AGEATINT_1984 
  rename R1602400 Q3_1_1985   // Q3-1
  rename R1604500 Q3_1B_1985   // Q3-1B
  rename R1778501 Q13_5_TRUNC_REVISED_1985   // Q13-5_TRUNC_REVISED
  rename R1778801 Q13_9_TRUNC_REVISED_1985   // Q13-9_TRUNC_REVISED
  rename R1890600 ESR_KEY_1985 
  rename R1890700 REGION_1985 
  rename R1890901 HGCREV85_1985 
  rename R1891010 AGEATINT_1985 
  rename R1902900 Q3_1_1986   // Q3-1
  rename R1905000 Q3_1B_1986   // Q3-1B
  rename R2141601 Q13_5_TRUNC_REVISED_1986   // Q13-5_TRUNC_REVISED
  rename R2141901 Q13_9_TRUNC_REVISED_1986   // Q13-9_TRUNC_REVISED
  rename R2257700 ESR_KEY_1986 
  rename R2257800 REGION_1986 
  rename R2258001 HGCREV86_1986 
  rename R2258110 AGEATINT_1986 
  rename R2303300 FAM_MOM_M_1987   // FAM-MOM_M
  rename R2303500 FAM_MOM_Y_1987   // FAM-MOM_Y
  rename R2303600 FAM_MOM_AGE_1987   // FAM-MOM_AGE
  rename R2303700 Q3_1_1987   // Q3-1
  rename R2305900 Q3_1B_1987   // Q3-1B
  rename R2350301 Q13_5_TRUNC_REVISED_1987   // Q13-5_TRUNC_REVISED
  rename R2350601 Q13_9_TRUNC_REVISED_1987   // Q13-9_TRUNC_REVISED
  rename R2445100 ESR_KEY_1987 
  rename R2445200 REGION_1987 
  rename R2445401 HGCREV87_1987 
  rename R2445510 AGEATINT_1987 
  rename R2505500 FAM_MOM_M_1988   // FAM-MOM_M
  rename R2505700 FAM_MOM_Y_1988   // FAM-MOM_Y
  rename R2505800 FAM_MOM_AGE_1988   // FAM-MOM_AGE
  rename R2505900 Q3_1_1988   // Q3-1
  rename R2508400 Q3_1B_1988   // Q3-1B
  rename R2722501 Q13_5_TRUNC_REVISED_1988   // Q13-5_TRUNC_REVISED
  rename R2722801 Q13_9_TRUNC_REVISED_1988   // Q13-9_TRUNC_REVISED
  rename R2737900 CRES_1_1988   // CRES-1
  rename R2837200 BMOM69_1988 
  rename R2837300 BMOM0_1988 
  rename R2839200 BDAD96_1988 
  rename R2839300 BDAD0_1988 
  rename R2870600 ESR_KEY_1988 
  rename R2870800 REGION_1988 
  rename R2871101 HGCREV88_1988 
  rename R2871300 AGEATINT_1988 
  rename R2905000 Q3_1_1989   // Q3-1
  rename R2907500 Q3_1B_1989   // Q3-1B
  rename R2971401 Q13_5_TRUNC_REVISED_1989   // Q13-5_TRUNC_REVISED
  rename R2971701 Q13_9_TRUNC_REVISED_1989   // Q13-9_TRUNC_REVISED
  rename R3074300 ESR_KEY_1989 
  rename R3074500 REGION_1989 
  rename R3074801 HGCREV89_1989 
  rename R3075000 AGEATINT_1989 
  rename R3107000 Q3_1_1990   // Q3-1
  rename R3109600 Q3_1B_1990   // Q3-1B
  rename R3279401 Q13_5_TRUNC_REVISED_1990   // Q13-5_TRUNC_REVISED
  rename R3279701 Q13_9_TRUNC_REVISED_1990   // Q13-9_TRUNC_REVISED
  rename R3401000 ESR_KEY_1990 
  rename R3401200 REGION_1990 
  rename R3401501 HGCREV90_1990 
  rename R3401700 AGEATINT_1990 
  rename R3507000 Q3_1_1991   // Q3-1
  rename R3509600 Q3_1B_1991   // Q3-1B
  rename R3559001 Q13_5_TRUNC_REVISED_1991   // Q13-5_TRUNC_REVISED
  rename R3559301 Q13_9_TRUNC_REVISED_1991   // Q13-9_TRUNC_REVISED
  rename R3656400 ESR_KEY_1991 
  rename R3656600 REGION_1991 
  rename R3656901 HGCREV91_1991 
  rename R3657100 AGEATINT_1991 
  rename R3707000 Q3_1_1992   // Q3-1
  rename R3709600 Q3_1B_1992   // Q3-1B
  rename R3897101 Q13_5_TRUNC_REVISED_1992   // Q13-5_TRUNC_REVISED
  rename R3897401 Q13_9_TRUNC_REVISED_1992   // Q13-9_TRUNC_REVISED
  rename R4006900 ESR_KEY_1992 
  rename R4007100 REGION_1992 
  rename R4007401 HGCREV92_1992 
  rename R4007600 AGEATINT_1992 
  rename R4134700 Q3_1_1993   // Q3-1
  rename R4137400 Q3_1B_1993   // Q3-1B
  rename R4295101 Q13_5_TRUNC_REVISED_1993   // Q13-5_TRUNC_REVISED
  rename R4295501 Q13_9_TRUNC_REVISED_1993   // Q13-9_TRUNC_REVISED
  rename R4418000 ESR_KEY_1993 
  rename R4418200 REGION_1993 
  rename R4418501 HGCREV93_1993 
  rename R4418700 AGEATINT_1993 
  rename R4523300 Q3_1_1994   // Q3-1
  rename R4526000 Q3_1B_1994   // Q3-1B
  rename R4982801 Q13_5_TRUNC_REVISED_1994   // Q13-5_TRUNC_REVISED
  rename R4983201 Q13_9_TRUNC_REVISED_1994   // Q13-9_TRUNC_REVISED
  rename R5081000 ESR_KEY_1994 
  rename R5081200 REGION_1994 
  rename R5081700 AGEATINT_1994 
  rename R5103900 HGCREV94_1994 
  rename R5166300 ESR_KEY_1996 
  rename R5166500 REGION_1996 
  rename R5166901 HGCREV96_1996 
  rename R5167000 AGEATINT_1996 
  rename R5221000 Q3_1_1996   // Q3-1
  rename R5221300 Q3_1B_1996   // Q3-1B
  rename R5626201 Q13_5_TRUNC_REVISED_1996   // Q13-5_TRUNC_REVISED
  rename R5626601 Q13_9_TRUNC_REVISED_1996   // Q13-9_TRUNC_REVISED
  rename R5821100 Q3_1_1998   // Q3-1
  rename R5821300 Q3_1B_1998   // Q3-1B
  rename R6364601 Q13_5_TRUNC_REVISED_1998   // Q13-5_TRUNC_REVISED
  rename R6365001 Q13_9_TRUNC_REVISED_1998   // Q13-9_TRUNC_REVISED
  rename R6478900 ESR_KEY_1998 
  rename R6479100 REGION_1998 
  rename R6479600 HGCREV98_1998 
  rename R6479800 AGEATINT_1998 
  rename R6539800 Q3_1_2000   // Q3-1
  rename R6540000 Q3_1B_2000   // Q3-1B
  rename R6909701 Q13_5_TRUNC_REVISED_2000   // Q13-5_TRUNC_REVISED
  rename R6911101 Q13_9_TRUNC_REVISED_2000   // Q13-9_TRUNC_REVISED
  rename R7006800 REGION_2000 
  rename R7007300 HGCREV00_2000 
  rename R7007500 AGEATINT_2000 
  rename R7103000 Q3_1_2002   // Q3-1
  rename R7103200 Q3_1B_2002   // Q3-1B
  rename R7607800 Q13_5_TRUNC_2002   // Q13-5_TRUNC
  rename R7609000 Q13_9_TRUNC_2002   // Q13-9_TRUNC
  rename R7704100 REGION_2002 
  rename R7704600 HGCREV02_2002 
  rename R7704800 AGEATINT_2002 
  rename R7809900 Q3_1_2004   // Q3-1
  rename R7810100 Q3_1B_2004   // Q3-1B
  rename R8316300 Q13_5_TRUNC_2004   // Q13-5_TRUNC
  rename R8318200 Q13_9_TRUNC_2004   // Q13-9_TRUNC
  rename R8496500 REGION_2004 
  rename R8497000 HGCREV04_2004 
  rename R8497200 AGEATINT_2004 
  rename T0013800 Q3_1_2006   // Q3-1
  rename T0014000 Q3_1B_2006   // Q3-1B
  rename T0912400 Q13_5_TRUNC_2006   // Q13-5_TRUNC
  rename T0913900 Q13_9_TRUNC_2006   // Q13-9_TRUNC
  rename T0988100 ESR_KEY_2006 
  rename T0988300 REGION_2006 
  rename T0988800 HGCREV06_2006 
  rename T0989000 AGEATINT_2006 
  rename T1213700 Q3_1_2008   // Q3-1
  rename T1213900 Q3_1B_2008   // Q3-1B
  rename T2076700 Q13_5_TRUNC_2008   // Q13-5_TRUNC
  rename T2078800 Q13_9_TRUNC_2008   // Q13-9_TRUNC
  rename T2210300 REGION_2008 
  rename T2210700 HGCREV08_2008 
  rename T2210800 AGEATINT_2008 
  rename T2272200 Q3_1_2010   // Q3-1
  rename T2272400 Q3_1B_2010   // Q3-1B
  rename T3045300 Q13_5_TRUNC_2010   // Q13-5_TRUNC
  rename T3047500 Q13_9_TRUNC_2010   // Q13-9_TRUNC
  rename T3108200 REGION_2010 
  rename T3108600 HGCREV10_2010 
  rename T3108700 AGEATINT_2010 
  rename T3212300 Q3_1_2012   // Q3-1
  rename T3212500 Q3_1B_2012   // Q3-1B
  rename T3977400 Q13_5_TRUNC_2012   // Q13-5_TRUNC
  rename T3979400 Q13_9_TRUNC_2012   // Q13-9_TRUNC
  rename T4112700 REGION_2012 
  rename T4113100 HGCREV12_2012 
  rename T4113200 AGEATINT_2012 
  rename T4200400 Q3_1_2014   // Q3-1
  rename T4200600 Q3_1B_2014   // Q3-1B
  rename T4915800 Q13_5_TRUNC_2014   // Q13-5_TRUNC
  rename T4917800 Q13_9_TRUNC_2014   // Q13-9_TRUNC
  rename T5023100 REGION_2014 
  rename T5023500 HGCREV14_2014 
  rename T5175400 Q3_1_2016   // Q3-1
  
  
*Sample restrictions, basics  
drop if inlist(SAMPLE_ID, 9,12,15,16,17,18,19,20) 
recode SAMPLE_RACE_78SCRN (2=1)(nonmiss=0), gen(black)
recode SAMPLE_RACE_78SCRN (3=1)(nonmiss=0), gen(white)
keep if black==1 | white==1

rename CASEID_1979 id
rename SAMPWEIGHT_1979 weight
recode SAMPLE_SEX_1979 (1=0)(2=1), gen(female)
rename FAM_POB_1979 bornsouth

*parent ed
rename  HGC_MOTHER_1979 momhgc  
rename  HGC_FATHER_1979  dadhgc 

recode momhgc (0/8=1)(nonmiss=0), gen(momed8)
recode momhgc (9/11=1)(nonmiss=0), gen(momed9_11)
recode momhgc (12=1)(nonmiss=0), gen(momed12)
recode momhgc (13/20=1)(nonmiss=0), gen(momedcollege)


*Birth order
rename FAM_28A sibcnt
rename FAM_28C birth_order
replace birth_order=birth_order+1
replace birth_order=1 if sibcnt==0
g high_border=birth_order>=4

*mother age at birth_order
g tmp1=FAM_MOM_AGE_1987-AGEATINT_1987
g tmp2=FAM_MOM_AGE_1988-AGEATINT_1988
egen mombyear=rowfirst(FAM_MOM_Y_1987 FAM_MOM_Y_1988)
egen kidbyear=rowfirst(Q1_3_A_Y_1979 Q1_3_A_Y_1981)
g tmp3=kidbyear-mombyear
egen mom_ageb=rowfirst(tmp1 tmp2 tmp3)



*farm household proxy
recode FAM_6 (3=1)(nonmiss=0), gen(farm)


*age at each interview
foreach y of numlist 1979(1)1994 1996(2)2012 {
rename AGEATINT_`y' age`y'
}


*total income from wages, salaries, business and farm
rename Q13_8_1979 Q13_9_1979 //rename a few own farm incomes to make NLS names consistant across waves
rename Q13_8_1981 Q13_9_1981
rename Q13_8_TRUNC_REVISED_1982 Q13_9_1982
rename Q13_8_TRUNC_REVISED_1984 Q13_9_1984

*combine wage income and farm/bus income for each yr. the universe for income questions in 79 was everyone, so zero means zero here and missing means nonresponse/refusal
foreach y of numlist 1979(1)1994 1996(2)2014 {
egen earnings`y'=rowtotal(Q13_5*`y' Q13_9*`y'), missing
}
drop Q13*


*enrollment status
foreach y of numlist 1979(1)1994 1996(2)2014 {
rename Q3_1B_`y' enrolled`y'
}
drop Q3_1*


*afqt scores
g afqtpctile=AFQT_3_1981/1000
egen afqtz=std(afqtpctile) 


*education
foreach y of numlist 1979(1)1994 1996(2)2014 {
rename HGCREV*`y' hgc`y'
}



*reshape
reshape long earnings  enrolled hgc  age  , i(id) j(year)

*tag observations after schooling completion
bysort id: egen lastyearenrolled=max(year) if enrolled==1
bysort id: egen lastyearfilled=max(lastyearenrolled)
replace earnings=. if year<lastyearfilled 
drop lastyearenrolled lastyearfilled

*finalize earnings measure
cpigen
replace cpi=cpi/1.44 //makes 2017 the base year
replace earnings=earnings/cpi 

bysort id: egen levelearnings=mean(earnings)
g logearnings=ln(levelearnings)

*highest ed ever observed
bysort id: egen hgc_ever=max(hgc)

keep id weight bornsouth momhgc momed8 momed9_11 momed12 momedcollege dadhgc sibcnt birth_order high_border mom_ageb  farm black white female levelearnings logearnings hgc_ever  afqtz afqtpctile
duplicates drop id, force //everything is now time-invariant

cd "C:\Users\ot3\Documents\CRFertility"

save nlsy79working, replace




























